'use strict';
import React from 'react';
import {connect} from 'dva/index';
import {Button, Form, Table} from 'antd';
import styles from '../index.less';
import FormBase from '../../components/Base/FormBase';
import ViewTitle from "../../components/ViewTitle";
import {ModalView} from "antd-extra";
import ModalCreateOrEditPersonnel from "./modal/ModalCreateOrEditPersonnel";


const _ = require('lodash');

const table = 'personnel';

@Form.create()
@connect(({
            user,
            global, personnel,
            treeNode
          }) => ({...user, ...global, ...personnel, ...treeNode}))
export default class Index extends FormBase {
  constructor(props) {
    super(props);
    this.state = {};
  }

  componentDidMount() {
    this.props.dispatch({
      type: 'global/getModel',
      payload: {table, tenantCode: this.getTenantCode()}
    });
    this.props.dispatch({
      type: 'personnel/findPersonnelMany',
      payload: {table, tenantCode: this.getTenantCode()}
    });
  }

  onCreateOrEdit = record => {
    const model = this.props[`${table}Model`];
    ModalView.open(ModalCreateOrEditPersonnel, {title: '创建表单', width: '1080px'}, {
      model, record,
      callback: payload => {
        payload.code = record.code;
        this.props.dispatch({type: 'personnel/updatePersonnel', payload}).then(res => {
          this.requestReturn(res);
          this.componentDidMount();
        });
      }
    });
  };
  onDelete = record => {
    this.props.dispatch({type: 'personnel/deletePersonnel', payload: record}).then(res => {
      this.requestReturn(res);
      this.componentDidMount();
    });
  };

  render() {
    const {loading} = this.state;
    const {personnelList = []} = this.props;
    const model = this.props[`${table}Model`];

    return <Form className={styles.content}>
      <ViewTitle title={'员工列表'}
                 actions={<Button
                   type="primary"
                   onClick={() => {
                     ModalView.open(ModalCreateOrEditPersonnel, {title: '添加员工信息', width: '1080px'}, {
                       model,
                       callback: payload => {
                         this.props.dispatch({type: 'personnel/createPersonnel', payload}).then(res => {
                           this.requestReturn(res);
                           this.componentDidMount();
                         });
                       }
                     });
                   }}>添加</Button>}
      ></ViewTitle>
      <Table
        loading={loading}
        className={styles.antTable}
        bordered={true}
        columns={this.getDicTableCols(model, this.onCreateOrEdit, this.onDelete)}
        dataSource={personnelList}
        scroll={{x: true}}
        pagination={{
          current: this.state.current,
          total: this.state.total,
          showSizeChanger: true,
          onChange: page => {
            this.state.current = page;
            this.componentDidMount();
          },
          onShowSizeChange: (current, pageSize) => {
            this.state.current = 1;
            this.state.pageSize = pageSize;
            this.componentDidMount();
          }
        }}
        // pagination={{pageSize: 100}}
        //scroll={{ x: 1300 }}
      ></Table>

    </Form>;
  }
}
